package com.printeverywhere.delegator;

import static com.printeverywhere.util.AdminConstants.CALLED_ADD_METHOD;
import static com.printeverywhere.util.AdminConstants.ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.ERROR_IN_ADDING_THE_RECORD;
import static com.printeverywhere.util.AdminConstants.GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.RECORD_ADDED;
import static com.printeverywhere.util.AdminConstants.SESSION_CLOSED;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

import com.printeverywhere.dto.PromotionalCode;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.HibernateUtil;

public class PromotionalCodesServiceDelegator {
	private static Logger logger = Logger
	.getLogger(PromotionalCodesServiceDelegator.class);

	/**
	 * Append the PromotionalCodes object as new row in PromotionalCodes
	 * table.
	 *
	 * @param promotionalCode
	 *            the General categories
	 *
	 * @return the string
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static String add(PromotionalCode promotionalCode)
			throws HibernateException {
		logger.info(CALLED_ADD_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.saveOrUpdate(promotionalCode);
			transaction.commit();
			logger.info(RECORD_ADDED);
			return RECORD_ADDED;
		} catch (HibernateException e) {
			logger.error(ERROR_IN_ADDING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.flush();
			session.close();
		}
	}

	/**
	 * Update the promotionalCode object as new row in PromotionalCode
	 * table.
	 *
	 * @param promotionalCode
	 *            the promotional Code
	 *
	 * @return the string
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static String update(PromotionalCode promotionalCode)
			throws HibernateException {
		logger.info(AdminConstants.CALLED_UPDATE_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.update(promotionalCode);
			transaction.commit();
			logger.info(AdminConstants.RECORD_UPDATED);
			return AdminConstants.RECORD_UPDATED;
		} catch (HibernateException e) {
			logger.error(AdminConstants.ERROR_IN_UPDATING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	/**
	 * Gets the promotionalCode object where id value matches with
	 * given id.
	 *
	 * @param id
	 *            the category id
	 *
	 * @return the promotional Code
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static PromotionalCode get(int id)
			throws HibernateException {
		logger.info("Called get method".intern());
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		PromotionalCode promotionalCode = new PromotionalCode();
		try {
			Criteria criteria = session.createCriteria(PromotionalCode.class)
					.add(Expression.eq(AdminConstants.ID, id));
			List<PromotionalCode> list = criteria.list();
			promotionalCode = (list.size() > 0) ? list.get(0)
					: new PromotionalCode();
			transaction.commit();
		} catch (HibernateException e) {
			logger.info("Error in getting the object for given id "
					.intern()
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
		return promotionalCode;
	}

	public static PromotionalCode getPromotionalCodeFromCode(String code) throws HibernateException {
		logger.info("Called get method".intern());
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		PromotionalCode promotionalCode = new PromotionalCode();
		try {
			Criteria criteria = session.createCriteria(PromotionalCode.class)
					.add(Expression.eq("promotionalCode", code));
			List<PromotionalCode> list = criteria.list();
			promotionalCode = (list.size() > 0) ? list.get(0)
					: null;
			transaction.commit();
		} catch (HibernateException e) {
			logger.info("Error in getting the object for given id ".intern()
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
		return promotionalCode;
	}

	/**
	 * Gets the list of all the records from PromotionalCode table without
	 * checking any predefined criteria, It simply list all the rows from
	 * database table.
	 *
	 * @return the list
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static List<PromotionalCode> getList() throws HibernateException {
		logger.info(GET_LIST_WITHOUT_SEARCH_CRITERIA);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			List<PromotionalCode> list = session.createCriteria(
					PromotionalCode.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			logger.error(ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}
}
